package lab.anoper.fsm.fsm;

import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;

/**
 * 支付订单状态机
 *
 * @author Yan Jiahong
 * Created at 2020/9/25
 */
@StateMachineParameters(stateType=PaymentState.class, eventType=PaymentEvent.class, contextType=Integer.class)
public class PaymentOrderStateMachine extends AbstractUntypedStateMachine {
    protected void fromNotPaidToPaying(PaymentState from, PaymentState to, PaymentEvent event, Integer context) {
        System.out.printf("Transition from [%s] to [%s] on event [%s] with context [%s]",
                from, to, event, context
        );
    }

    protected void fromPayingToPaySuccess(PaymentState from, PaymentState to, PaymentEvent event, Integer context) {
        System.out.printf("Transition from [%s] to [%s] on event [%s] with context [%s]",
                from, to, event, context
        );
    }

    protected void fromPayingToPayFailed(PaymentState from, PaymentState to, PaymentEvent event, Integer context) {
        System.out.printf("Transition from [%s] to [%s] on event [%s] with context [%s]",
                from, to, event, context
        );
    }

    public static void main(String[] args) {
        UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(PaymentOrderStateMachine.class);
        builder.externalTransition().from(PaymentState.NOT_PAID).to(PaymentState.PAYING)
                .on(PaymentEvent.PAY_INITIALIZED_SUCCESS).callMethod("fromNotPaidToPaying");
        UntypedStateMachine paymentFSM = builder.newStateMachine(PaymentState.NOT_PAID);

        paymentFSM.fire(PaymentEvent.PAY_INITIALIZED_SUCCESS, 0);

        System.out.println("现在的状态是: " + paymentFSM.getCurrentState());
    }
}



















